﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using gds_classes;
using GCD;

namespace e_bank
{
    public class Bank
    {
        private List<int> UsedCoins = new List<int>();

        private int Q { get; set; }
        private int P { get; set; }
        public int d { get; set; }

        public int Balance { get; set; }

        private int f
        {
            get
            {
                return (this.Q - 1) * (this.P - 1);
            }
        }

        public int N
        {
            get
            {
                return this.Q*this.P;
            }
        }

        private int c
        {
            get
            {
                return MathMethods.FindInversion(d, this.f);
            }
        }

        public int Rate { get; private set; }

        public Bank(int p, int q, int rate)
        {
            this.Q = q;
            this.P = p;
            this.Rate = rate;

            d = 2;
            while (gcd.GCDSimple(d, f) != 1 && d < f)   // Weak
            {
                d++;
            }
            if (d >= f)
            {
                throw new Exception();
            }
        }

        public int GetCash(int n_k)
        {
            if (Balance - Rate >= 0)
            {
                Balance -= Rate;
                return MathMethods.PowByMod(n_k, c, N);
            }
            else
            {
                return -1;
            }
            
        }

        public bool ValidateCash(int[] cash)
        {
            int n=cash[0];
            int s_k=cash[1];
            if (UsedCoins.Contains(n))
            {
                return false;
            }
            else
            {
                UsedCoins.Add(n);
            }
            return (n / 10 == n % 10) && (MathMethods.PowByMod(n, d, N) == s_k);
        }
    }
}
